package com.prs.crm.dao.crm.impl;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.crm.DispatchDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.crm.Dispatch;
import com.prs.crm.domain.crm.mainBill;


public class DispatchDaoImpl extends BaseDaoImpl<Dispatch> implements DispatchDao{
	

	

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Dispatch> getAdressToDispatch(final String adress) {
		return (List<Dispatch>) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = arg0.createCriteria(Dispatch.class);
						criteria.add(Restrictions.eq("transportAddress", adress));
						return criteria.list();
					}
					
				}
			);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Dispatch> getDateToDispatch(final String transportDate) {
		return (List<Dispatch>) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = arg0.createCriteria(Dispatch.class);
						criteria.add(Restrictions.eq("transportDate", transportDate));
						return criteria.list();
					}
					
				}
			);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Dispatch> getNameToDispatch(final String name) {
		return (List<Dispatch>) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = arg0.createCriteria(Dispatch.class);
						criteria.add(Restrictions.eq("receivePerson", name));
						return criteria.list();
					}
					
				}
			);
	}



	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Dispatch> getNumToDispatch(final String num) {
		return (List<Dispatch>) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = arg0.createCriteria(Dispatch.class);
						criteria.add(Restrictions.eq("dishfRecod", num));
						return criteria.list();
					}
					
				}
			);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Dispatch> getNumpageToDispatch(final String num,final String page) {
		return (List<Dispatch>) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = arg0.createCriteria(Dispatch.class);
						criteria.add(Restrictions.eq("dishfRecod", num));
						criteria.add(Restrictions.eq("dispageNum", page));
						return criteria.list();
					}
					
				}
			);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Dispatch> getNameandIdToDispatch(final String name,final int id) {
		return (List<Dispatch>) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = arg0.createCriteria(Dispatch.class);
						criteria.add(Restrictions.eq("receivePerson", name));
						criteria.add(Restrictions.gt("id", id));
						return criteria.list();
					}
					
				}
			);
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Dispatch> getByNameandByAdress(final String name,final String adress) {
		return (List<Dispatch>) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = arg0.createCriteria(Dispatch.class);
						criteria.add(Restrictions.eq("receivePerson",name));
						criteria.add(Restrictions.eq("transportAddress",adress));
						return criteria.list();
					}
					
				}
			);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Dispatch> getByNameInfor(final String name) {
		return (List<Dispatch>) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = arg0.createCriteria(Dispatch.class);
						criteria.add(Restrictions.eq("receivePerson",name));						
						return criteria.list();
					}
					
				}
			);
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Dispatch> getByCurrentTimeData(final Date startTime,final Date endTime) {
		return (List<Dispatch>) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = arg0.createCriteria(Dispatch.class);
						criteria.add(Restrictions.between("recordTime", startTime, endTime));						
						return criteria.list();
					}
					
				}
			);
	}

	


	
}
